package com.ww.springboot.boot.thread.study.executor;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-02-25 11:32
 */
public class LinkedBlockingQueueDemo1 {

    private static final ExecutorService executorService = new ThreadPoolExecutor(8, 8, 60, TimeUnit.SECONDS, new LinkedBlockingQueue());

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //线程安全
        LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(6);

        Future future = executorService.submit(() -> {
            System.out.println("----------add start-------------");
            for (int i = 0; i < 10; i++) {
                //添加数据 如果队列已满 则抛异常
                linkedBlockingQueue.add(i);
            }
            System.out.println("----------add end-------------");
        });

        executorService.execute(() -> {
            System.out.println("----------put start-------------");
            for (int i = 0; i < 10; i++) {
                //添加数据 如果队列已满 则阻塞等待
                try {
                    linkedBlockingQueue.put(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("----------put end-------------");
        });
        executorService.execute(() -> {
            System.out.println("----------offer start-------------");
            for (int i = 0; i < 10; i++) {
                //添加数据 如果队列已满 则返回false
                linkedBlockingQueue.offer(i);
            }
            System.out.println("----------offer end-------------");
        });

        future.get();
    }
}
